package br.com.viamais.model;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import br.com.viamais.infra.model.BaseEntity;

/**
 * <B>Project: viamais-web</B><BR>
 * 
 * Entity class that represents the Profile of the User.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Sep 18, 2011
 * @version 1.0
 */
@Entity
@Table(name = "tb_profile")
public class Profile implements BaseEntity {

	/**
	 * Serial version ID.
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "id_profile", nullable = false, unique = true, updatable = true)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	/**
	 * Profile name.
	 */
	@Column(name = "cl_name", nullable = false, unique = true, updatable = true)
	private String name;
	/**
	 * User list
	 */
	@OneToMany(mappedBy = "profile")
	private Set<User> users;
	
	/**
	 * Permission list.
	 */
	@JoinTable(name = "profile_role", 
			   joinColumns = { @JoinColumn(name = "id_profile") }, 
			   inverseJoinColumns = { @JoinColumn(name = "id_role") })
	@ManyToMany(cascade=CascadeType.ALL)
	private Set<Role> roles;

	/**
	 * Default constructor.
	 */
	public Profile() {
		super();
	}

	/**
	 * Constructor with fields.
	 * @param name
	 * @param users
	 * @param permissions
	 */
	public Profile(String name, Set<User> users, Set<Role> roles) {
		super();
		this.name = name;
		this.users = users;
		this.roles = roles;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public Integer getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the users
	 */
	public Set<User> getUsers() {
		return users;
	}

	/**
	 * @param users the users to set
	 */
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	public void deleteUsers() {
		if(this.users != null) {
			for (User user : users) {
				user.setProfile(null);
			}
		}
	}

	/**
	 * @param user
	 */
	public void addUser(User user) {
		user.setProfile(this);
		this.users.add(user);
	}

	/**
	 * @return the permissions
	 */
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * @param permissions the permissions to set
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
	/**
	 * @param role
	 */
	public void addRole(Role role, List<Profile> profiles) {
		if(roles == null) {
			roles = new HashSet<Role>();
		}
		role.setProfiles(profiles);
		this.roles.add(role);
	}
	
	public void deleteRoles() {
		if(this.roles != null) {
			for (Role role : roles) {
				role.setProfiles(null);
			}
			this.setRoles(null);
		}
	}
	
	/**
	 * @param role
	 */
	public void addRole(Role role) {
		if(roles == null) {
			roles = new HashSet<Role>();
		}
		this.roles.add(role);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Profile)) {
			return false;
		}
		Profile other = (Profile) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Profile [name=" + name + "]";
	}
	
}
